// app.js
import { config } from "./config/config";
App({ 
  Url: 'https://david.james8bond.top', //优蓝商城
  // Url: "https://youmei.ublue.xyz",  //优美
  globalData: {
    username: "",
    phone: ""
  },
  async onLaunch() {
    
  },
  login: async function (callback) {
    // 发送 res.code 到后台换取 openId, sessionKey, unionId
    wx.login({
      success: (res) => {
        wx.request({
          url: `${config.apiBaseUrl}wechat/index/login`,
          data: {
            code: res.code,
          },
          success: (res_1) => {

            // console.log(res_1.data.data.token);
            let token = res_1.data.data.token;
            // cache('token', token)
            wx.setStorageSync('token', token);
            wx.request({
              url: `${config.apiBaseUrl}wechat/index/init`,
              data: {
                token: token,
              },
              success: (res_2) => {
                let arr = res_2.data.data;
                // console.log(arr);
                callback && callback(res_2.data.data)
              }
            });
            // callback && callback(res_1)
          }
        });
      },
    })
  },

  getToken: function (callback) {
    wx.login({
      success: (res) => {
        wx.request({
          url: `${config.apiBaseUrl}wechat/index/login`,
          data: {
            code: res.code,
          },
          success: (res_1) => {
            console.log(res_1);
            // console.log(res_1.data.data.token);
            let token = res_1.data.data.token;
            // cache('token', token)
            wx.setStorageSync('token', token);

            callback && callback(token)

            // wx.request({
            //   url: `${config.apiBaseUrl}wechat/index/init`,
            //   data: {
            //     token: token,
            //   },
            //   success: (res_2) => {
            //     let arr = res_2.data.data;
            //     // console.log(arr);
            //     callback && callback(res_2.data.data)
            //   }
            // });

            // callback && callback(res_1)
          }
        });
      },
    })
  }

})